Uncrossed lines

Time: O(MxN); Space: O(MIN(M,N)); medium

We write the integers of A and B (in the order they are given) on two separate horizontal lines.

Now, we may draw connecting lines: a straight line connecting two numbers A[i] and B[j] such that:

A[i] == B[j]; The line we draw does not intersect any other connecting (non-horizontal) line. Note that a connecting lines cannot intersect even at the endpoints: each number can only belong to one connecting line.

Return the maximum number of connecting lines we can draw in this way.

Example 1:

Input: A = [1,4,2], B = [1,2,4]

Output: 2

Explanation:

  • We can draw 2 uncrossed lines as in the diagram.

  • We cannot draw 3 uncrossed lines, because the line from A[1]=4 to B[2]=4 will intersect the line from A[2]=2 to B[1]=2.

Example 2:

Input: A = [2,5,1,2,5], B = [10,5,2,1,5,2]

Output: 3

Example 3:

Input: A = [1,3,7,1,7,5], B = [1,9,2,5,1]

Output: 2

Constraints:

  • 1 <= len(A) <= 500

  • 1 <= len(B) <= 500

  • 1 <= A[i], B[i] <= 2000

Hints:

  1. Think dynamic programming. Given an oracle dp(i,j) that tells us how many lines A[i:], B[j:] [the sequence A[i], A[i+1], … and B[j], B[j+1], …] are uncrossed, can we write this as a recursion?

1. Dynamic programming [O(MxN), O(min(M,N))]

[1]:
class Solution1(object):
    """
    Time: O(M*N)
    Space: O(MIN(M,N))
    """
    def maxUncrossedLines(self, A, B):
        """
        :type A: List[int]
        :type B: List[int]
        :rtype: int
        """
        if len(A) < len(B):
            return self.maxUncrossedLines(B, A)

        dp = [[0 for _ in range(len(B)+1)] for _ in range(2)]

        for i in range(len(A)):
            for j in range(len(B)):
                dp[(i+1)%2][j+1] = max(dp[i%2][j] + int(A[i] == B[j]),
                                       dp[i%2][j+1],
                                       dp[(i+1)%2][j])

        return dp[len(A)%2][len(B)]
[2]:
s = Solution1()
A = [1,4,2]
B = [1,2,4]
assert s.maxUncrossedLines(A, B) == 2

A = [2,5,1,2,5]
B = [10,5,2,1,5,2]
assert s.maxUncrossedLines(A, B) == 3

A = [1,3,7,1,7,5]
B = [1,9,2,5,1]
assert s.maxUncrossedLines(A, B) == 2